package com.daikit.daikit4gxt.client.action.base;

import java.util.List;

import com.daikit.commons.shared.bean.DkLanguage;
import com.daikit.commons.shared.miscs.Couple;
import com.daikit.commons.shared.utils.DkStringUtils;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.daikit.daikit4gxt.client.log.DkLogger;


/**
 * Get the action to execute to load current Language and available languages for the application.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkBaseLoadCurrentAndAvailableLanguagesAction
{

	/**
	 * @return the action to execute to load current Language and available languages for the application.
	 */
	public static final DkBaseAction<?> get()
	{
		final DkBaseAction<Couple<DkLanguage, List<DkLanguage>>> actionLoad = new DkBaseAction<Couple<DkLanguage, List<DkLanguage>>>(
				"", false)
		{
			@Override
			protected void run()
			{
				DkBaseMain.controller().baseRpcMiscs()
						.getCurrentAndAvailableLanguages(new DkBaseAsyncCallback<Couple<DkLanguage, List<DkLanguage>>>(this, true)
						{
							@Override
							public void processSuccess(final Couple<DkLanguage, List<DkLanguage>> result)
							{
								DkBaseMain.model().setCurrentLanguage(result.first);
								DkBaseMain.model().getAvailableLanguages().update(result.second);
								String languagesAsString = "";
								if (result.second != null)
								{
									for (final DkLanguage language : result.second)
									{
										languagesAsString = DkStringUtils.joinsep(", ", languagesAsString, language.getIsocode());
									}
								}
								DkLogger.debug(getClass(), "Current language="
										+ (result.first == null ? null : result.first.getIsocode()) + ", available languages=["
										+ languagesAsString + "]");
							}
						});
			}

		};
		return actionLoad;
	}

}
